package com.just521.xinshoutong;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.baidu.location.LocServiceMode;
import com.baidu.location.LocationChangedListener;
import com.baidu.location.LocationClient;
import com.baidu.location.ReceiveListener;

public class GlobalVar extends Application {

	public LocationClient mLocationClient = null;
	public String province;
	public String city;
	public String district;
	byte[] glocker;
	LocListener mlistener;
	int mloccount=0;
	
	
//	private String mData;
//	TextView mTv;
	
	@Override
	public void onCreate() {
		mLocationClient = new LocationClient(this);
		startLoc();
		glocker=new byte[0];
	}
	
	public void startLoc(){
		mLocationClient.setProdName("xinshou");
		mLocationClient.setCoorType("bd09ll");
		mLocationClient.setAddrType("province|city|district");
		mLocationClient.setServiceMode(LocServiceMode.Immediat);
		mLocationClient.addRecerveListener(new MyReceiveListenner());
		mLocationClient.start();
	}
	public void requestLoc(LocListener listener){
		logMsg("requestLoc");
		if(province!=null&&province.length()!=0){
//			mLocationClient.stop();
			return;
		}
		mloccount++;
		mLocationClient.getLocation();
		mlistener=listener;
	}

	public void logMsg(String str) {
		try {
			Log.e("zds", str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class MyLocationChangedListener implements LocationChangedListener {
		@Override
		public void onLocationChanged() {
			logMsg("LocationChangedListener: ");
		}
	}

	public class MyReceiveListenner implements ReceiveListener {
		@Override
		public void onReceive(String strData) {
			logMsg("ReceiveListener: " + strData);
			if(strData==null || strData.length()==0 || strData.equals("InternetException")){
				mhandler.sendEmptyMessage(LocEvent_err);
				return;
			}
			
			try{
				JSONObject ob = new JSONObject(strData);
				JSONObject jso= ob.getJSONObject("content");
				JSONObject jsaddr= jso.getJSONObject("addr");
				province=jsaddr.getString("province");
				if(province==null || province.length()==0){
					mhandler.sendEmptyMessage(LocEvent_err);
					return;
				}
				city=jsaddr.getString("city");
				if(city==null || city.length()==0){
					mhandler.sendEmptyMessage(LocEvent_err);
					return;
				}
				district=jsaddr.getString("district");
				if(district==null || district.length()==0){
					mhandler.sendEmptyMessage(LocEvent_err);
					return;
				}
//				UserInfo.put("province", province);
//				UserInfo.put("city", city);
//				UserInfo.put("region", district);
				
				mhandler.sendEmptyMessage(LocEvent_ok);
//				if(glocker!=null){
//					glocker.notify();
//				}
//				mLocationClient.stop();
			}catch(JSONException e){
				mhandler.sendEmptyMessage(LocEvent_err);
				e.printStackTrace();
			}
		}
	}
	public interface LocListener{
		public void onRegionChanged();
	}
	final int LocEvent_ok=0;
	final int LocEvent_err=1;
	Handler mhandler=new MyHandler();
	class MyHandler extends Handler {  
  	  
        public MyHandler() {  
        }  
  
        @Override  
        public void handleMessage(Message msg) {  
        	
            switch (msg.what) {  
           
            case LocEvent_ok:  
            	if(province==null || province.length()==0){
            		mloccount++;
                	mLocationClient.getLocation();
                	break;
            	}
//            	mLocationClient.stop();
            	if(mlistener!=null){
            		mLocationClient.stop();
            		mlistener.onRegionChanged();
            	}
                break;  
            case LocEvent_err:  
            	if(mloccount>4){
//            		mLocationClient.stop();
            		if(mlistener!=null){
                		mlistener.onRegionChanged();
                	}
            		return;
            	}
            	mloccount++;
            	mLocationClient.getLocation();
                break; 
            default:  
                break;  
            }  
        }  
    };
}